package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.d_c.model.Seller;
import com.zr.d_c.model.User;
import com.zr.d_c.service.OrderService;
import com.zr.d_c.service.SellerOrderService;
import com.zr.d_c.service.SellerService;
import com.zr.d_c.serviceimpl.OrderServiceImpl;
import com.zr.d_c.serviceimpl.SellerOrderServiceImpl;
import com.zr.d_c.serviceimpl.SellerServiceImpl;
/**
 * 商家接受订单
 * @author Administrator
 *
 */
public class AcceptOrderController extends HttpServlet{
	SellerOrderService sorder = new SellerOrderServiceImpl();
	OrderService orderservice = new OrderServiceImpl();
	SellerService sellerservice = new SellerServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// TODO Auto-generated method stub
	doPost(req, resp);
}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int i = Integer.parseInt(req.getParameter("count"));
		HttpSession session = req.getSession();
		PrintWriter pw = resp.getWriter();
		if(i>0){
			for(int j = 0;j<i;j++){
				int oid = Integer.parseInt(req.getParameter("orders["+j+"][oid]"));
				sorder.acceptOrderByOid(oid);
			}
			User u = (User) session.getAttribute("currentUser");
			int currentOrder = (Integer) session.getAttribute("currentOrder");
			int uid = u.getUid();
			Seller s = sellerservice.getSellerByUid(uid);
			int sid = s.getSid();
			int orderNum = orderservice.getCountCurrentOrderBySid(sid);
			if(currentOrder != orderNum){
				session.setAttribute("currentOrder", orderNum);
			}
			pw.write("1");
		}else{
			pw.write("0");
		}
	}

}
